/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.sppp;

import com.max.oti.system.bean.IndexLoginBean;
import com.max.oti.system.model.ReportPractice;
import com.max.oti.system.service.interfac.IReportPracticeService;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.omnifaces.util.Faces;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author max
 */
@ManagedBean
@SessionScoped
public class LibraryBean {
    
    @ManagedProperty("#{reportPracticeService}")
    IReportPracticeService reportPracticeService;
    @ManagedProperty("#{indexLoginBean}")
    IndexLoginBean indexLoginBean;
    //////////////////////////////////////////////////
    private List<ReportPractice> listReportPractice = new ArrayList<ReportPractice>();
    private List<ReportPractice> listReportPracticeFilter;
    ///////////////////////////////////////
    private ReportPractice reportPractice;
    ////////////////////////////////////////
    public void initBean() {
        indexLoginBean.setNavigation("./content/library_practice.xhtml");
        String HQL = "select rp.id as id, rp.idPractice as idPractice, rp.fileUrl as fileUrl from ReportPractice rp where rp.fileUrl!='***'";
        System.out.println(HQL);
        try {
            listReportPractice = reportPracticeService.listHQL(HQL, ReportPractice.class);
            System.out.println(listReportPractice.size());
            listReportPracticeFilter = null;
        } catch (Exception ex) {
            Logger.getLogger(LibraryBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public StreamedContent getFileDowload() {
        
        InputStream stream = Faces.getResourceAsStream("/resources/files/" + reportPractice.getFileUrl());  
        StreamedContent  file = new DefaultStreamedContent(stream, "application/pdf", "archivo.pdf");  
        return file;
    }
    
    
    
    //<editor-fold defaultstate="collapsed" desc="Getter and Setter">

    public List<ReportPractice> getListReportPractice() {
        return listReportPractice;
    }

    public IndexLoginBean getIndexLoginBean() {
        return indexLoginBean;
    }

    public void setIndexLoginBean(IndexLoginBean indexLoginBean) {
        this.indexLoginBean = indexLoginBean;
    }

    public void setReportPracticeService(IReportPracticeService reportPracticeService) {
        this.reportPracticeService = reportPracticeService;
    }

    public void setListReportPractice(List<ReportPractice> listReportPractice) {
        this.listReportPractice = listReportPractice;
    }

    public ReportPractice getReportPractice() {
        return reportPractice;
    }

    public void setReportPractice(ReportPractice reportPractice) {
        this.reportPractice = reportPractice;
    }
    
    public List<ReportPractice> getListReportPracticeFilter() {
        return listReportPracticeFilter;
    }

    public void setListReportPracticeFilter(List<ReportPractice> listReportPracticeFilter) {
        this.listReportPracticeFilter = listReportPracticeFilter;
    }
    //</editor-fold>
}
